import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// 后端类型配置 - 可以通过环境变量覆盖
const BACKEND_TYPE = process.env.VITE_BACKEND_TYPE || 'nodejs'
const NODEJS_PORT = process.env.VITE_NODEJS_PORT || '3000'
const GO_PORT = process.env.VITE_GO_PORT || '3001'

// 根据后端类型选择端口
const getBackendPort = () => {
  switch (BACKEND_TYPE) {
    case 'go':
      return GO_PORT
    case 'nodejs':
    default:
      return NODEJS_PORT
  }
}

const backendPort = getBackendPort()

console.log(`🔗 前端代理配置: ${BACKEND_TYPE} 后端 -> http://127.0.0.1:${backendPort}`)

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  server: {
    host: '0.0.0.0',
    port: 5173,
    proxy: {
      '/api': {
        target: `http://127.0.0.1:${backendPort}`,
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, '/api')
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'axios']
        }
      }
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  }
}) 